package listener;

import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import view.MainFrame;
import controller.LayerListController;
import controller.LayeredPaneController;

public class ListListener implements MouseListener,MouseMotionListener{

    private int m_fusionIndex;
    private Cursor m_previousCursor;
	private int m_startIndex;
    
    public ListListener(){
        m_fusionIndex = -1;
    }

    /**
     * used to fusion layers
     * <br> used to reverse layers
     */
    @Override
    public void mouseReleased(MouseEvent arg0) {
    	 MainFrame.getInstance().setCursor(m_previousCursor);
        if (arg0.getButton() == MouseEvent.BUTTON1){
            if (m_fusionIndex != -1){
                LayeredPaneController.fusionLayers(m_fusionIndex,MainFrame.getInstance().getLayerList().locationToIndex(arg0.getPoint()));
            }
            else{
                 LayerListController.reverseLayers(m_startIndex,MainFrame.getInstance().getLayerList().locationToIndex(arg0.getPoint()));
            }
            m_fusionIndex = -1;
        }
    }
    
     /**
     * used to fusion layers
     * <br> used to reverse layers
     */
    @Override
    public void mousePressed(MouseEvent arg0) {
    	m_previousCursor = MainFrame.getInstance().getCursor();
        MainFrame.getInstance().setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
        if (arg0.getButton() == MouseEvent.BUTTON1){
            if (arg0.isAltDown()){
                MainFrame.getInstance().setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                m_fusionIndex = MainFrame.getInstance().getLayerList().locationToIndex(arg0.getPoint());
                System.out.println("alt");
            }
            else{
                MainFrame.getInstance().setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
                m_startIndex = MainFrame.getInstance().getLayerList().locationToIndex(arg0.getPoint());
            }
        }
    }
    
    //---- NOT USE ------//
    
	@Override
	public void mouseDragged(MouseEvent arg0) {
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

}
